package com.example.gpstest.Utils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.example.gpstest.activity.GPSGoogleEarthActivity;
import com.example.gpstest.activity.GpsTtffActivity;
import com.example.gpstest.activity.MainActivity;

import org.dom4j.Element;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Namespace;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

//生成Kml文件集合类
public class KmlUtils {
    /**
     * 初始化
     * @param coordinates(经纬度：经度,纬度,高度)代表一个地理信息的对象集合（自定义）
     * @param fileName 生成kml的名称
     */
    public static boolean setTravelsKml(String[] coordinates, String fileName) throws IOException {
        Element root = DocumentHelper.createElement("kml");
        Document document = DocumentHelper.createDocument(root);
        //根节点添加属性
        /*这里用namespace是因为当时xmlns这个属性添加不上去，所以用这个方法可行
         * Namespace namespace = Namespace.get("http://www.opengis.net/kml/2.2");
         *  root.add(namespace);
         * */
        Namespace namespace = Namespace.get("http://www.opengis.net/kml/2.2");
         root.addAttribute("xmlns", "http://www.opengis.net/kml/2.2");
        // .addAttribute("xmlns:gx", "http://www.google.com/kml/ext/2.2")
        //  .addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
        // .addAttribute("xsi:schemaLocation",
        //     "http://www.opengis.net/kml/2.2 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd http://www.google.com/kml/ext/2.2 http://code.google.com/apis/kml/schema/kml22gx.xsd");
        root.add(namespace);
        Element documentElement = root.addElement("Document");
        // documentElement.addElement("name").addText(fileName);
        // documentElement.addElement("Snippet").addText("");
        // Element folderElement = documentElement.addElement("Document");//添加一个目录
        // folderElement.addAttribute("id", "FeatureLayer0");
        documentElement.addElement("name").addText("轨迹点位"); //名称
        //folderElement.addElement("Snippet").addText(""); //显示在Google Earth之中的对description的简短概要.


        //生成点位图标数据
        int i = 1;
         for (String travelRecord:coordinates) {
          i++;
        Element placeMarkElement = documentElement.addElement("Placemark");//在文件夹中添加一个地标
        //placeMarkElement.addAttribute("id", String.valueOf(i));
        placeMarkElement.addElement("name").addText("点位" + String.valueOf(i));
        placeMarkElement.addElement("styleUrl").addText("#randomColorRegion");
        // placeMarkElement.addElement("Snippet").addText("");
        placeMarkElement.addElement("description").addText("R");//简介
        //placeMarkElement.addElement("styleUrl").addText("#MyStyle");//风格
        Element pointElement = placeMarkElement.addElement("Polygon");
        // Element pointElement = placeMarkElement.addElement("Point");
        Element outerBoundaryIs1 = pointElement.addElement("outerBoundaryIs");
        Element LinearRing1 = outerBoundaryIs1.addElement("LinearRing");
        //分解
        // String[] con = travelRecord.split(",");
        //添加点位的经纬度坐标以及高度(显示时绘制高度m)
        LinearRing1.addElement("coordinates").addText(coordinates[i]);//可以是是任何几何形状的子元素，定义每一个点的经度、纬度和高度(按照严格的顺序). 多个点使用空格隔开，经纬度按照WGS84标准.
         }


        //生成轨迹线路径数据
        Element lineElement = documentElement.addElement("Placemark");//在文件夹外添加一个地标
        lineElement.addElement("name").addText("轨迹线");
        lineElement.addElement("description").addText("");
        lineElement.addElement("styleUrl").addText("#MyStyle");
        Element pointElement1 = lineElement.addElement("LineString");
        pointElement1.addElement("altitudeMode").addText("absolute");
        pointElement1.addElement("extrude").addText("1");
        pointElement1.addElement("tessellate").addText("1");
        String linedata = "";
        //每个坐标以及高度用换行符或空格分开
//        for (TravelRecord travelRecord:travelRecords) {
//            linedata =linedata+"\n"+ travelRecord.lng+","+travelRecord.lat+",30";
//        }
//        pointElement.addElement("coordinates").addText(linedata);
        //生成显示风格
        Element styleElement = documentElement.addElement("Style");//Style节点
        styleElement.addAttribute("id", "MyStyle");
        // IconStyle 图标风格
        Element iconStyleElement = styleElement.addElement("IconStyle");
         Element iconElement = iconStyleElement.addElement("Icon");
	        iconElement.addElement("href").addText("http://192.168.10.108:8080/images/mark_b.png");//在线图标
        iconStyleElement.addElement("scale").addText("0.250000");
        // LabelStyle  标签风格
         Element labelStyleElement = styleElement.addElement("LabelStyle");
         labelStyleElement.addElement("color").addText("00000000");
        labelStyleElement.addElement("scale").addText("0.000000");
        // PolyStyle 图形风格
//        Element Style = placeMarkElement.addElement("Style");
//        Element polyStyleElement = Style.addElement("PolyStyle");
//        polyStyleElement.addElement("color").addText("96ffff00");
//        polyStyleElement.addElement("outline").addText("0");

        // LineStyle  路径线风格
       Element lineStyleElement = styleElement.addElement("LineStyle");
        lineStyleElement.addElement("color").addText("7f00ffff");
        lineStyleElement.addElement("width").addText("4");

        //创建kml到本地
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("D:/web/airportData/" + fileName + "_1.kml"), format);

        xmlWriter.write(document);

        xmlWriter.close();
        //开始对文件进行压缩，一个kml文件其实是一个压缩文件，里面包含一个kml文件和一个png图标
        String[] strs = new String[1];
        strs[0] = "D:/web/Travels/" + fileName + "_1.kml";
//        strs[1] = "D:/web/Travels/images/img_mark.png";//这里写图片的路径  如果使用在线图标这段代码屏蔽
        //writeKml(strs, "D:/web/Travels/" + fileName);//压缩

        return true;

    }


    /**
     * 初始化
     * @param args 经纬度、高度集合
     */
    public static void TravelsKml(ArrayList<String> args, String kmlName, Context context) throws Exception {
        Element root = DocumentHelper.createElement("kml");  //根节点是kml
        Document document = DocumentHelper.createDocument(root);
        //给根节点kml添加属性
        root.addAttribute("xmlns", "http://www.opengis.net/kml/2.2")
                .addAttribute("xmlns:gx", "http://www.google.com/kml/ext/2.2")
                .addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
                .addAttribute("xsi:schemaLocation",
                        "http://www.opengis.net/kml/2.2 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd http://www.google.com/kml/ext/2.2 http://code.google.com/apis/kml/schema/kml22gx.xsd");

        //给根节点kml添加子节点  Document
        Element documentElement = root.addElement("Document");

        documentElement.addElement("name").addText("kml文件名"); //添加name节点
        documentElement.addElement("Snippet").addText(""); //Snippet节点
        Element folderElement = documentElement.addElement("Folder");//Folder节点
        folderElement.addAttribute("id", "FeatureLayer0");
        //给Folder节点添加子节点
        folderElement.addElement("name").addText("kml文件名");
        folderElement.addElement("Snippet").addText("");
        //循环添加每一个Placemark节点，有几个坐标点就有几个Placemark节点，这里仅作示例
//            for (int i = 0; i < args.size(); i++) {
//                Element placeMarkElement = folderElement.addElement("Placemark");
//                placeMarkElement.addAttribute("id", String.valueOf(i));
//                placeMarkElement.addElement("name").addText("坐标点");
//                placeMarkElement.addElement("Snippet").addText("");
//                placeMarkElement.addElement("description").addText("");
//                placeMarkElement.addElement("styleUrl").addText("#IconStyle00");
//                Element pointElement = placeMarkElement.addElement("Point");
//                pointElement.addElement("altitudeMode").addText("clampToGround");
//                //添加每一个坐标点的经纬度坐标，这里仅作示例
//                pointElement.addElement("coordinates").addText(args.get(i));
//            }

        //生成轨迹线路径数据
        Element lineElement = documentElement.addElement("Placemark");//在文件夹外添加一个地标
        lineElement.addElement("name").addText("轨迹线");
        lineElement.addElement("description").addText("");
        lineElement.addElement("styleUrl").addText("#IconStyle00");
        Element pointElement1 = lineElement.addElement("LineString");
        pointElement1.addElement("altitudeMode").addText("absolute");
        pointElement1.addElement("extrude").addText("1");
        pointElement1.addElement("tessellate").addText("1");
        String linedata = "";
        //每个坐标以及高度用换行符或空格分开
        for (int i = 0; i < args.size(); i++) {
            linedata = linedata +"\n"+args.get(i);
            pointElement1.addElement("coordinates").addText(args.get(i));
            //pointElement1.addElement("coordinates").addText(linedata);
        }

        Element styleElement = documentElement.addElement("Style");//Style节点
        styleElement.addAttribute("id", "IconStyle00");
        // IconStyle
        Element iconStyleElement = styleElement.addElement("IconStyle");
        Element iconElement = iconStyleElement.addElement("Icon");
        iconElement.addElement("href").addText("Layer0_Symbol_107dde08.png");
        iconStyleElement.addElement("scale").addText("0.250000");
        // LabelStyle
        Element labelStyleElement = styleElement.addElement("LabelStyle");
        labelStyleElement.addElement("color").addText("00000000");
        labelStyleElement.addElement("scale").addText("0.000000");
        // PolyStyle
        Element polyStyleElement = styleElement.addElement("PolyStyle");
        polyStyleElement.addElement("color").addText("ff000000");
        polyStyleElement.addElement("outline").addText("0");
        // LineStyle  路径线风格
        Element lineStyleElement = styleElement.addElement("LineStyle");
        lineStyleElement.addElement("color").addText("7f00ffff");
        lineStyleElement.addElement("width").addText("4");
        //将kml写出本地

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");//设置编码格式

        String filePath = Environment.getExternalStorageDirectory().toString()+"/GPSGoogleEarth";
        File destFile = new File(filePath ,kmlName);
        destFile.createNewFile();

        FileOutputStream fileOutputStream=new FileOutputStream(destFile,true);
        XMLWriter xmlWriter = new XMLWriter(fileOutputStream,format);

        xmlWriter.write(document);

        xmlWriter.close();
        //开始对文件进行压缩，一个kml文件其实是一个压缩文件，里面包含一个kml文件和一个png图标

        String[] strs = new String[1];
        strs[0]="/storage/emulated/0/GPSGoogleEarth/"+kmlName;
//            strs[1]="Layer0_Symbol_107dde08.png";//这里写图片的路径
        writeKml(strs, "压缩kml文件",context);
        System.out.println("success");


    }
    /**
     * 初始化
     * @param strs 压缩的kml文件集合
     * @param kmlName 文件名称
     */
    public static void writeKml(String[] strs, String kmlName,Context context) throws IOException
        {
            String[] files = strs;
            //File kmlFile = new File("person.kml");
            OutputStream os = new BufferedOutputStream( new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/GPSGoogleEarth/"+kmlName + ".kml"));
            ZipOutputStream zos = new ZipOutputStream(os);
            byte[] buf = new byte[8192];
            int len;
            for (int i=0;i < files.length;i++) {
                File file = new File(files[i]);
                if ( !file.isFile() )
                    continue;
                ZipEntry ze = new ZipEntry( file.getName() );
                zos.putNextEntry( ze );
                BufferedInputStream bis = new BufferedInputStream( new FileInputStream( file ) );
                while ( ( len = bis.read( buf ) ) > 0 ) {
                    zos.write( buf, 0, len );
                    Toast.makeText(context, "保存成功", Toast.LENGTH_LONG).show();
                }
                zos.closeEntry();
            }

            zos.closeEntry();
            zos.close();
        }


    /**
     * 获取现在时间
     *
     * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
     */
    public static String getStringDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getStringDates() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }
}
